package com.hang.util.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hang.mapper.ClazzStudentMapper;
import com.hang.mapper.StudentMapper;
import com.hang.model.Student;
import com.hang.vo.StudentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

@Slf4j
public class StudentListener extends AnalysisEventListener<StudentVo> {
    private StudentMapper studentMapper;

    public StudentListener(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    //一行一行读取
    @Override
    public void invoke(StudentVo studentVo, AnalysisContext analysisContext) {
        if(!ObjectUtils.isEmpty(studentVo.getStudentId())){
            //调用方法添加数据库
            Student student = new Student();
            //简单算法：提取身份证后六位作为默认密码
            student.setPassword(studentVo.getIdentity().substring(studentVo.getIdentity().length()-6));

            BeanUtils.copyProperties(studentVo,student);
            studentMapper.insert(student);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("学生信息导入完成");
    }
}